package com.hn.kiss.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hn.kiss.modules.sys.dto.RoleJurisdictionTreeDTO;
import com.hn.kiss.modules.sys.entity.RoleJurisdiction;
import com.hn.kiss.modules.sys.mapper.RoleJurisdictionMapper;
import com.hn.kiss.modules.sys.service.IRoleJurisdictionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限角色分配 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2020-08-06
 */
@Service
public class RoleJurisdictionServiceImpl extends ServiceImpl<RoleJurisdictionMapper, RoleJurisdiction> implements IRoleJurisdictionService {

    @Override
    public RoleJurisdiction getByPk(String roleCode, String jurisdictionId) {
        LambdaQueryWrapper<RoleJurisdiction> lambda = new QueryWrapper<RoleJurisdiction>().lambda()
                .eq(RoleJurisdiction::getRoleCode, roleCode)
				.eq(RoleJurisdiction::getJurisdictionId, jurisdictionId);
        return baseMapper.selectOne(lambda);
    }

    @Override
    public boolean updateByPk(RoleJurisdiction roleJurisdiction) {
        LambdaQueryWrapper<RoleJurisdiction> lambda = new QueryWrapper<RoleJurisdiction>().lambda()
                .eq(RoleJurisdiction::getRoleCode, roleJurisdiction.getRoleCode())
				.eq(RoleJurisdiction::getJurisdictionId, roleJurisdiction.getJurisdictionId());
        return super.update(roleJurisdiction, lambda);
    }

    @Override
    public boolean removeByPk(String roleCode, String jurisdictionId) {
        LambdaQueryWrapper<RoleJurisdiction> lambda = new QueryWrapper<RoleJurisdiction>().lambda()
                .eq(RoleJurisdiction::getRoleCode, roleCode)
				.eq(RoleJurisdiction::getJurisdictionId, jurisdictionId);
        return super.remove(lambda);
    }

    @Override
    public List<String> listJuriUrlByRole(String roleCode) {
        return baseMapper.selectJuriUrlByRole(roleCode);
    }

    @Override
    public List<RoleJurisdictionTreeDTO> tree(String roleCode) {
        List<RoleJurisdictionTreeDTO> jurisdictionTreeDTOS = baseMapper.selectJuriByRoleAndParentId(roleCode, "0");
        return treeAll(jurisdictionTreeDTOS, roleCode);
    }

    @Transactional
    @Override
    public boolean allocation(String roleCode, String... juriIds) {
        //先将角色权限全部删除,在添加
        super.remove(Wrappers.<RoleJurisdiction>lambdaQuery().eq(RoleJurisdiction::getRoleCode, roleCode));


        ArrayList<RoleJurisdiction> list = new ArrayList<>();
        for (String juriId : juriIds) {
            if (StringUtils.isBlank(juriId))
                continue;

            RoleJurisdiction roleJurisdiction = new RoleJurisdiction().setRoleCode(roleCode).setJurisdictionId(juriId);
            list.add(roleJurisdiction);
        }
        super.saveBatch(list);
        return true;
    }











    /**
     * 获取去权限树形数据
     * @param juris
     * @return
     */
    private List<RoleJurisdictionTreeDTO> treeAll(List<RoleJurisdictionTreeDTO> juris, String roleCode){
        if (juris == null || juris.size() == 0)
            return null;

        juris.forEach(item -> {
            List<RoleJurisdictionTreeDTO> list = baseMapper.selectJuriByRoleAndParentId(roleCode, item.getId());
            if (list != null && list.size() > 0)
                item.setChildren(treeAll(list, roleCode));
        });
        return juris;
    }

}
